// ignore_for_file: deprecated_member_use

import 'package:flutter/material.dart';
import 'package:flutter_widget/custom/custom_appbar.dart';
import 'package:signature/signature.dart';

// ignore: camel_case_types, must_be_immutable
class SignaturePage extends StatefulWidget {
  String name;
  SignaturePage({Key? key, required this.name}) : super(key: key);

  @override
  _SignaturePageState createState() => _SignaturePageState();
}

// ignore: camel_case_types
class _SignaturePageState extends State<SignaturePage> {
  final bool _isSign = false;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CustomAppbar(widget.name, Theme.of(context).primaryColor),
      backgroundColor: Colors.grey,
      body: Container(
        width: double.infinity,
        padding: const EdgeInsets.all(10),
        child: Column(
          children: [
            Container(
              color: Colors.white,
              width: double.infinity,
              padding: const EdgeInsets.all(12),
              margin: const EdgeInsets.all(12),
              child: Column(
                children: [
                  buildWidget(),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: [
                      InkWell(
                        onTap: () {
                          // ignore: avoid_print
                          print('清除');
                          _SignatureController.clear();
                        },
                        child: const Icon(Icons.clear),
                      ),
                    ],
                  ),
                ],
              ),
            ),
            ElevatedButton(
              onPressed: () async {
                final image = await _SignatureController.toImage(); // 生成图片
                // ignore: avoid_print
                print(image);
              },
              child: const Text(
                '保存',
                style: TextStyle(
                  fontSize: 20,
                  color: Colors.white,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  // ignore: non_constant_identifier_names
  final SignatureController _SignatureController = SignatureController(
    penStrokeWidth: 5.0, // 画笔粗细
    penColor: Colors.orange, // 画笔颜色
  );

  Widget buildWidget() {
    return Container(
      constraints: const BoxConstraints(maxHeight: 200),
      alignment: Alignment.center,
      // ignore: prefer_const_literals_to_create_immutables
      child: Stack(
        // ignore: prefer_const_literals_to_create_immutables
        alignment: Alignment.center,
        // ignore: prefer_const_literals_to_create_immutables
        children: [
          Offstage(
            offstage: _isSign,
            child: const Text(
              '签名',
              style: TextStyle(
                  fontSize: 25,
                  color: Colors.grey,
                  fontWeight: FontWeight.w600),
            ),
          ),
          Signature(
            controller: _SignatureController,
            backgroundColor: Colors.white,
          ),
        ],
      ),
    );
  }
}
